package ink.lovejinhu.leetcode;

import java.util.HashSet;

/**
 * @author jinhu
 * created 2021-07-13 11:23
 */
public class Solution_0345 {
    /**
     * 反转字符串中的元音字母
     *
     * @param s
     * @return
     */
    public String reverseVowels(String s) {
        HashSet<Character> characters = new HashSet<>();
        characters.add('a');
        characters.add('e');
        characters.add('i');
        characters.add('o');
        characters.add('u');

        characters.add('A');
        characters.add('E');
        characters.add('I');
        characters.add('O');
        characters.add('U');
        char[] chars = s.toCharArray();
        for (int i = 0, j = chars.length - 1; i < j; ) {
            if (characters.contains(chars[i])) {  //左边是元音
                if (characters.contains(chars[j])) {   //右边是元音
                    swap(chars, i, j);
                    i++;
                    j--;
                } else {
                    j--;
                }
            } else {
                if (characters.contains(chars[j])) {   //右边是元音
                    i++;
                } else {
                    j--;
                    i++;
                }

            }

        }
        return new String(chars);
    }

    public void swap(char[] param, int left, int right) {
        char temp = param[left];
        param[left] = param[right];
        param[right] = temp;


    }
}
